package com.pic.mgt.haishin.manager.terminal;

import com.pic.mgt.manager.terminal.AbstractTerminalManagerListener;
import com.pic.mgt.manager.terminal.BasicTerminalInfoBean;
import com.pic.mgt.manager.terminal.TerminalManagementListener;
import com.pic.mgt.monitor.UnitMonitor;
import com.pic.mgt.monitor.UnitMonitorException;

public final class HaishinTerminalManagementListener extends
		AbstractTerminalManagerListener implements TerminalManagementListener {
	
	private UnitMonitor messageUnitMonitor;
	
	@Override
	public void afterSynchronizeTerminalNotify() {
		
	}
	
	@Override
	public void afterSynchronizeAddedTerminalNotify(
			BasicTerminalInfoBean addedBasicTerminalInfoBean) {
		/* 
		 * 當一個端末因同步而做的新增動作完成後:
		 * (1) 新增端末的訊息(Message)監控
		 */
		
		// (1) 新增端末的訊息(Message)監控
		String haishinOutboxPathStrForNewTerminalId = super
			.getHaishinOutboxPathStrForTerminalId(addedBasicTerminalInfoBean.getTerminalId());
		try {
			this.messageUnitMonitor
				.addMonitorPath(haishinOutboxPathStrForNewTerminalId);

		} catch (UnitMonitorException e) {
			// 不用寫 Log
		}
	}
	
	@Override
	public void afterDynamicAddedTerminalNotify(BasicTerminalInfoBean addedBasicTerminalInfoBean) {
		/* 
		 * 當一個端末動態新增完成後:
		 * (1) 新增端末的訊息(Message)監控
		 */
		
		// (1) 新增端末的訊息(Message)監控
		String haishinOutboxPathStrForNewTerminalId = super
			.getHaishinOutboxPathStrForTerminalId(addedBasicTerminalInfoBean.getTerminalId());
		try {
			this.messageUnitMonitor
				.addMonitorPath(haishinOutboxPathStrForNewTerminalId);

		} catch (UnitMonitorException e) {
			// 不用寫 Log
		}
	}
	
	@Override
	public void beforeDynamicRemovedTerminalNotify(String toBeRemovedTerminalIdStr) {
		/*
		 *  當一個端末將動態被移除前:
		 *  (1) 移除端末的訊息(Message)監控
		 */
		
		// (1) 移除端末的訊息(Message)監控
		String haishinOutboxPathStrForToBeRemovedTerminalId = super
				  .getHaishinOutboxPathStrForTerminalId(toBeRemovedTerminalIdStr);
		this.messageUnitMonitor
			.removeMonitorPath(haishinOutboxPathStrForToBeRemovedTerminalId);
	}
	
	@Override
	public void beforeSynchronizeRemovedTerminalNotify(
			String toBeRemovedTerminalIdStr) {
		/*
		 *  當一個端末因同步而將被進行移除動作前:
		 *  (1) 移除端末的訊息(Message)監控
		 */
		
		// (1) 移除端末的訊息(Message)監控
		String haishinOutboxPathStrForToBeRemovedTerminalId = super
				  .getHaishinOutboxPathStrForTerminalId(toBeRemovedTerminalIdStr);
		this.messageUnitMonitor
			.removeMonitorPath(haishinOutboxPathStrForToBeRemovedTerminalId);
	}
	
	@Override
	public void terminalOutOfDateNotify(
			BasicTerminalInfoBean outOfDayBasicTerminalInfoBean) {
		// 當一個端末失效時，要做的動作就是移除端末的訊息(Message)監控
		
		String haishinOutboxPathStrForToBeRemovedTerminalId = super
			.getHaishinOutboxPathStrForTerminalId(outOfDayBasicTerminalInfoBean.getTerminalId());
		this.messageUnitMonitor
			.removeMonitorPath(haishinOutboxPathStrForToBeRemovedTerminalId);
	}
	
	// --- getter and setter ---

	public void setMessageUnitMonitor(UnitMonitor messageUnitMonitor) {
		this.messageUnitMonitor = messageUnitMonitor;
	}

}
